/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Group
 */
@Entity
public class Question implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String title;
    
    @ManyToOne
    private Course course;
    
    @OneToMany(mappedBy = "question", fetch= FetchType.EAGER)
    private List<Answer> answers;
    
    @OneToOne
    private Answer goodAnswer;
    
    public Answer getGoodAnswer() {
        return goodAnswer;
    }

    public void setGoodAnswer(Answer goodAnswer) {
        this.goodAnswer = goodAnswer;
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @XmlTransient
    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public List<Answer> getAnswers() {
        for (int i = 0; i < 5 * answers.size(); ++i) {
            int pos1 = (int) Math.ceil(Math.random() * answers.size())-1;
            int pos2 = (int) Math.ceil(Math.random() * answers.size())-1;
            
            Answer tmp = answers.get(pos1);
            
            answers.set(pos1, answers.get(pos2));
            answers.set(pos2, tmp);
        }
        
        return answers;
    }

    public void setAnswers(List<Answer> answers) {
        this.answers = answers;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Question)) {
            return false;
        }
        Question other = (Question) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.jmooc.entity.Question[ id=" + id + " ]";
    }
    
}
